/*
 * Copyright 2009 the original author or authors.
 * Copyright 2009 SorcerSoft.org.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sorcer.ssb.tools.plugin.browser;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import net.jini.admin.Administrable;
import net.jini.admin.JoinAdmin;
import net.jini.core.discovery.LookupLocator;
import net.jini.core.lookup.ServiceRegistrar;

public class FederateDialog extends javax.swing.JDialog 
	implements ActionListener{
    
    JFrame frame;
    
    /** Creates new form FederateDialog */
    public FederateDialog(JFrame parent,ArrayList lus) {
        super(parent, "Federate Lookup services", true);
        frame=parent;
        initComponents(lus.toArray());
        ItemListener il=new ItemListener(){
        	public void itemStateChanged(ItemEvent evt){
        		boolean same=src.getSelectedIndex()==dest.getSelectedIndex();
        		ok.setEnabled(!same);
        	}
        };
        
        src.addItemListener(il);
        dest.addItemListener(il);
    }
    public void actionPerformed(ActionEvent evt){
    	boolean isBidirectional=bidirectioal.isSelected();
    	ServiceNode srcNode=(ServiceNode)src.getSelectedItem();
    	ServiceNode destNode=(ServiceNode)dest.getSelectedItem();
    	
    	if(srcNode==destNode){
    		Toolkit.getDefaultToolkit().beep();
    		return;
    	}
    	
    	ServiceRegistrar srcProxy=(ServiceRegistrar)srcNode.getProxy();
    	ServiceRegistrar destProxy=(ServiceRegistrar)destNode.getProxy();
    	try{
	    	JoinAdmin srcJa=(JoinAdmin)((Administrable)srcProxy).getAdmin();
	    	JoinAdmin destJa=(JoinAdmin)((Administrable)destProxy).getAdmin();
	    	destJa.addLookupLocators(new LookupLocator[]{srcProxy.getLocator()});
	    	if(isBidirectional){
	    		srcJa.addLookupLocators(new LookupLocator[]{destProxy.getLocator()});
	    	}
	    	
    	}catch(Exception ex){
    		JOptionPane.showMessageDialog(frame,ex,"SSB: exception",JOptionPane.WARNING_MESSAGE);
    	}

    	
    	dispose();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents(Object [] lus) {//GEN-BEGIN:initComponents
        jLabel1 = new javax.swing.JLabel();
        src = new javax.swing.JComboBox(lus);
        jLabel2 = new javax.swing.JLabel();
        dest = new javax.swing.JComboBox(lus);
        dest.setSelectedIndex(1);
        bidirectioal = new javax.swing.JCheckBox();
        ok = new javax.swing.JButton();
        ok.addActionListener(this);
        cancel = new javax.swing.JButton();
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent evt){
				dispose();
			}
		});
        getContentPane().setLayout(null);

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jLabel1.setText("Source Lookup Service");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(10, 10, 210, 15);

        getContentPane().add(src);
        src.setBounds(10, 30, 220, 24);

        jLabel2.setText("Destination Lookup Service");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(10, 70, 220, 15);

        getContentPane().add(dest);
        dest.setBounds(10, 90, 220, 24);

        bidirectioal.setText("Bi-directional");
        bidirectioal.setToolTipText("Federate both lookup services with each other");
        bidirectioal.setIconTextGap(10);
        getContentPane().add(bidirectioal);
        bidirectioal.setBounds(250, 30, 150, 23);

        ok.setText("OK");
        getContentPane().add(ok);
        ok.setBounds(255, 60, 88, 25);

        cancel.setText("Cancel");
        getContentPane().add(cancel);
        cancel.setBounds(255, 90, 88, 25);

        pack();
    }//GEN-END:initComponents
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    /**
     * @param args the command line arguments
     */
     /*
    public static void main(String args[]) {
       FederateDialog dlg= new FederateDialog(new javax.swing.JFrame(), true);
       dlg.setResizable(false);
       dlg.setSize(385,170);
       dlg.setVisible(true);
    }
    */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox bidirectioal;
    private javax.swing.JButton cancel;
    private javax.swing.JComboBox dest;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton ok;
    private javax.swing.JComboBox src;
    // End of variables declaration//GEN-END:variables
    
}
